<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

if( isset( $_POST[ 'officer_id' ] ) && is_numeric( $_POST[ 'officer_id' ] ) ) {
	$current_turn = getCurrentTurn();
	$officer_id = $_POST[ 'officer_id' ];
	$query = "SELECT last_scouting, scouting, ruler_id " .
			"FROM officers " .
			"WHERE officer_id = $officer_id";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'ruler_id' ] ) && $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] && $row[ 'scouting' ] == 5 && $current_turn - $row[ 'last_scouting' ] >= 12 ) {
		$army_id = -1;
		if( isset( $_POST[ 'army_id' ] ) && is_numeric( $_POST[ 'army_id' ] ) ) {
			$army_id = $_POST[ 'army_id' ];
			$free_scan = false;
			$query = "SELECT faction_id " .
					"FROM faction_member " .
					"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( isset( $row[ 'faction_id' ] ) ) {
				$query2 = "SELECT faction_id " .
						"FROM faction_member " .
						"JOIN army ON army.ruler_id = faction_member.ruler_id " .
						"WHERE army_id = $army_id AND invited = 0 AND requested = 0";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				if( isset( $row2[ 'faction_id' ] ) && $row2[ 'faction_id' ] == $row[ 'faction_id' ] ) {
					$free_scan = true;
				}
				
				$query = "SELECT army.* " .
						"FROM army " .
						"JOIN army_visible ON army.army_id = army_visible.army_id " .
						"WHERE army_visible.ruler_id IN " .
							"( SELECT faction_member.ruler_id " .
							"FROM faction_member " .
							"WHERE faction_id = " . $row[ 'faction_id' ] . " AND invited = 0 AND requested = 0 ) " .
							" AND army.army_id = $army_id";
			}
			else {
				$query = "SELECT army.* " .
						"FROM army " .
						"JOIN army_visible ON army.army_id = army_visible.army_id " .
						"WHERE army_visible.ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND army.army_id = $army_id";
			}
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( isset( $row[ 'army_id' ] ) ) {
				$subject = translate( 'Gathered army information on' ) . ' ' . $row[ 'name' ];
				$text = translate( 'Information about' ) . ' ' . $row[ 'name' ] . ' ' . translate( 'owned by' ) . ' ';
				if( $row[ 'ruler_id' ] == -1 ) {
					$name = translate( 'Independent forces' );
					$faction_name = '';
				}
				else {
					$query2 = "SELECT name " .
							"FROM ruler " .
							"WHERE ruler_id = " . $row[ 'ruler_id' ];
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
					$name = $row2[ 'name' ];
					$query2 = "SELECT name " .
							"FROM faction " .
							"JOIN faction_member ON faction_member.faction_id = faction.faction_id " .
							"WHERE ruler_id = " . $row[ 'ruler_id' ] . " AND invited = 0 AND requested = 0 " .
							"LIMIT 1";
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
					if( isset( $row2[ 'name' ] ) ) {
						$faction_name = ' ' . translate( 'from the faction' ) . ' ' . $row2[ 'name' ];
					}
					else {
						$faction_name = '';
					}
				}
				$text .= $name . $faction_name . "\n";
				$x = round( $row[ 'actual_x' ] );
				$y = round( $row[ 'actual_y' ] );
				$text .= translate( 'Gathered at location' ) . ': <a href="../operations/map.php?x=' . ( $x - 20 ) . '&y=' . ( $y - 20 ) . '" title="' . translate( 'Show on the map' ) . '">(' . $x . ', ' . $y . ')</a>.' . "\n\n";
				$text .= translate( 'Composition' ) . ":\n";
				$text .= translate( 'Scouts' ) . ': <strong class="scouts">' . number_format( $row[ 'scout' ] ) . "</strong>\n";
				$text .= translate( 'Infantry' ) . ': <strong class="infantry">' . number_format( $row[ 'infantry' ] ) . "</strong>\n";
				$text .= translate( 'Cavalry' ) . ': <strong class="cavalry">' . number_format( $row[ 'cavalry' ] ) . "</strong>\n";
				$text .= translate( 'Artillery' ) . ': <strong class="artillery">' . number_format( $row[ 'artillery' ] ) . "</strong>\n\n";
				$text .= translate( 'Score' ) . ': ' . number_format( $row[ 'scout' ] * 32 + $row[ 'infantry' ] * 48 + $row[ 'cavalry' ] * 256 + $row[ 'artillery' ] * 400 ) . "\n\n";
				$text .= translate( 'Orders' ) . ":\n";
				
				$current_date = getCurrentDate();
				$current_turn = getCurrentTurn();
				$query2 = "SELECT * " .
					"FROM army_orders " .
					"WHERE army_id = $army_id AND order_type = 1 AND start_turn > 0 AND start_turn <= $current_turn " .
					"ORDER BY rank";
				$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
				$num = mysql_numrows($result2);
				$i = 0;
				if( $num > 0 ) {
					$calc_turn = $current_turn;
					$current_x = $row[ 'x' ];
					$current_y = $row[ 'y' ];
					while ($i < $num ) {
						$text .= ( $i + 1 ) . ' ';
						
						if( mysql_result( $result2, $i, "order_type" ) == 1 ) {
							$order_type = translate( 'Move to' );
							if( $calc_turn > 0 ) {
								if( mysql_result( $result2, $i, 'complete_turn' ) == 0 ) {
									$travel_time = getTravelTime( $army_id, mysql_result( $result2, $i, 'order_value' ), mysql_result( $result2, $i, 'order_value2' ), $current_x, $current_y, false, $row[ 'ruler_id' ] );
									if( $travel_time > 0 ) {
										$calc_turn += $travel_time + 1;
									}
									else {
										$travel_time = '';
										$calc_turn = -1;
									}
								}
								else {
									$calc_turn += mysql_result( $result2, $i, 'complete_turn' ) - $current_turn;
								}
							}
							$current_x = mysql_result( $result2, $i, 'order_value' );
							$current_y = mysql_result( $result2, $i, 'order_value2' );
							$query3 = "SELECT name, village_id, ruler_id " .
									"FROM village " .
									"WHERE x = " . mysql_result( $result2, $i, 'order_value' ) . " " .
									"AND y = " . mysql_result( $result2, $i, 'order_value2' );
							$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
							$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
							if( isset( $row3[ 'village_id' ] ) ) {
								$village_name = $row3[ 'name' ];
								$village_status = getVillageStatus( $row3[ 'village_id' ] );
								if( $village_status == 3 ) {
									$village_name = translate( 'Independent village' );
								}
								$color = getStatusColor( $village_status );
								if( $village_status == 0 ) {
									$description = "<a href=\"../villages/village.php?name=$village_name\"><strong id=\"$color\">$village_name</strong></a> <a href=\"../operations/map.php?x=" . ( mysql_result( $result2, $i, "order_value" ) - 20 ) . 
													"&y=" . ( mysql_result( $result2, $i, "order_value2" ) - 20 ) . "\" title=\"" . translate( 'To the map' ) . "\">" .
													"<strong id=\"$color\">(" . mysql_result( $result2, $i, "order_value" ) . ", " . mysql_result( $result2, $i, "order_value2" ) . ")</strong></a>";
								}
								else {
									$description = "<a href=\"../operations/map.php?x=" . ( mysql_result( $result2, $i, "order_value" ) - 20 ) . 
													"&y=" . ( mysql_result( $result2, $i, "order_value2" ) - 20 ) . "\" title=\"" . translate( 'To the map' ) . "\">" .
													"<strong id=\"$color\">$village_name (" . mysql_result( $result2, $i, "order_value" ) . ", " . mysql_result( $result2, $i, "order_value2" ) . ")</strong></a>";
								}
							}
							else {
								$description = "<a href=\"../operations/map.php?x=" . ( mysql_result( $result2, $i, "order_value" ) - 20 ) . 
												"&y=" . ( mysql_result( $result2, $i, "order_value2" ) - 20 ) . "\" title=\"" . translate( 'To the map' ) . "\">" .
												"(" . mysql_result( $result2, $i, "order_value" ) . ", " . mysql_result( $result2, $i, "order_value2" ) . ")</a>";
							}
						}
						elseif( mysql_result( $result2, $i, 'order_type' ) == 2 ) {
							if( $calc_turn > 0 ) {
								if( mysql_result( $result2, $i, 'complete_turn' ) == 0 ) {
									$calc_turn += mysql_result( $result2, $i, 'order_value' );
								}
								else {
									$calc_turn += mysql_result( $result2, $i, 'complete_turn' ) - $current_turn;
								}
							}
							$order_type = translate( 'Wait' );
							if( mysql_result( $result2, $i, 'order_value' ) == 1 ) {
								$description = translate( 'for 1 day' );
							}
							else {
								$description = translate( 'for' ) . " " . mysql_result( $result2, $i, 'order_value' ) . " " . translate( 'days' );
							}
						}
						
						$text .= $order_type . ' - ' . $description . ' - ';
		
						if( $calc_turn > 0 ) {
							$eta = $calc_turn - $current_turn;
							if( $eta == 1 ) {
								$eta = "$eta " . translate( "day" );
							}
							else {
								$eta = "$eta " . translate( "days" );
							}
							$text .= $eta;
						}
						else {
							$text .= translate( 'Not available' );
						}
						
						$text .= "\n";
						$i ++;
					}
				}
				else {
					$text .= translate( 'Unknown' );
				}
				$query2 = "INSERT INTO news " .
					"( subject, news, ruler_id, date, turn ) " .
					"VALUES( '$subject', '" . mysql_real_escape_string( $text ) . "', " . $_SESSION[ 'ruler_id' ] . ", '$current_date', $current_turn )";
				mysql_query($query2) or die("Query failed : " . mysql_error());
				if( !$free_scan ) {
					$query2 = "UPDATE officers " .
						"SET last_scouting = $current_turn " .
						"WHERE officer_id = $officer_id";
					mysql_query($query2) or die("Query failed : " . mysql_error());
				}
?>
					<pre><?php print( $text ); ?></pre><br />
					<?php transprint( 'A news item with this information has been created.' ); ?>
<?php
			}
			else {
				$status = translate( 'Invalid army id supplied.' );
			}	
		}
		else {
			$status = translate( 'Invalid officer id supplied.' );
		}
	}
	else {
		$status = translate( 'Incorrect call to this page.' );
	}
}
else {
	$status = translate( 'Incorrect call to this page.' );
}

if( isset( $status ) ) {
	print( $status );
}
?>
